﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QuanLyBanHang.DAL;
using QuanLyBanHang_NewApp.Entity;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using QuanLyBanHang_NewApp.DAL;


namespace QuanLyBanHang_NewApp.BUS
{
    public class NhanVienBUS
    {
        NhanVienDAL nvDal;

        public NhanVienBUS()
        {
            nvDal = new NhanVienDAL();
        }
        public NhanVien VerifyUser(string tk, string mk)
        {
            try
            {
                DataTable dt = new DataTable();
                dt = nvDal.checkUser(tk, mk);
                if (dt.Rows.Count > 0)
                {                    
                    int MaNV = int.Parse(dt.Rows[0][0].ToString());
                    string hoten = dt.Rows[0][1].ToString();
                    int quyen = int.Parse(dt.Rows[0][7].ToString());
                    int luong = int.Parse(dt.Rows[0][2].ToString());
                    string diachi = dt.Rows[0][3].ToString(); ;
                    string sodt = dt.Rows[0][4].ToString(); ;
                    string taikhoan = dt.Rows[0][5].ToString(); ;
                    string matkhau = dt.Rows[0][6].ToString(); ;
                    NhanVien nvien = new NhanVien(MaNV, hoten, luong, diachi, sodt, taikhoan, matkhau, quyen);
                    return nvien;
                }
                else return null;
            }
            catch
            {
                MessageBox.Show("Sai tài khoản hoặc mật khẩu");
                return null;
            }
        }
        //public int trungTK(string tk)
        //{
        //    DataTable dt = new DataTable();
        //    dt = nv_dal.getTK(tk);
        //    if ((dt != null) && (dt.Rows.Count > 0)) return dt.Rows.Count;
        //    return 0;
        //}
        //public string layTK(int  id)
        //{
        //    DataTable dt = new DataTable();
        //    dt = nv_dal.getTKfromID(id);
        //    if ((dt != null) && (dt.Rows.Count > 0)) return dt.Rows[0][0].ToString();
        //    return null;
        //}

        public bool themUser(NhanVien nv)
        {
            if (nvDal.isExist(nv.getAttribute(5))) return false;
            string hoten = nv.getAttribute(1);
            long luong = long.Parse(nv.getAttribute(2));
            string dc = nv.getAttribute(3);
            string sodt = nv.getAttribute(4);
            string tk = nv.getAttribute(5);
            string mk = nv.getAttribute(6);
            int quyen = int.Parse(nv.getAttribute(7));

            return nvDal.insertUser(hoten, luong, dc, sodt, tk, mk, quyen);
        }

        public DataTable getAllUser() {
            return nvDal.selectUser("");
        }

        public bool suaUser(NhanVien nv)
        {
            string hoten = nv.getAttribute(1);
            long luong = long.Parse(nv.getAttribute(2));
            string dc = nv.getAttribute(3);
            string sodt = nv.getAttribute(4);
            string tk = nv.getAttribute(5);
            string mk = nv.getAttribute(6);
            int quyen = int.Parse(nv.getAttribute(7));

            long manv = long.Parse(nv.getAttribute(0));

            return nvDal.updateUser(manv, hoten, luong, dc, sodt, tk, mk, quyen);
        }

        public bool xoaUser(long id)
        {
            return nvDal.deleteUser(id);
        }

        public DataTable getUserByName(string name)
        {
            return nvDal.selectUser(name);
        }

    }
}

